package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.ProcessExchangeMapper;
import com.indusfo.spc.pojo.ProcessExchange;
import com.indusfo.spc.service.ProcessExchangeService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author louk
 * @date 2019/9/2 10:38
 */
@Service
public class ProcessExchangeServiceImpl implements ProcessExchangeService {
    private static final Logger logger = LoggerFactory.getLogger(ProcessExchangeServiceImpl.class);
    @Resource
    private ProcessExchangeMapper processExchangeMapper;

    /**
     * 分页查询 pagesize pageindex
     * 模糊查询 dim
     * ID查询  Pid
     * 返回总数 count
     * @param processExchange
     * @return
     */
    @Override
    public JSONObject queryProcessExchange(ProcessExchange processExchange) {
        try {
            //获取页面的数据个数
            Integer pagesize = processExchange.getPagesize();
            //获取查询的页数
            Integer pageindex = processExchange.getPageindex();

            if(pagesize!=null && pageindex !=null){
                processExchange.setIncept(pagesize*(pageindex-1));
            }
            List<ProcessExchange> listProcessBom=processExchangeMapper.queryProcessExchange(processExchange);
            if(listProcessBom.isEmpty()){
                return  JSONObject.oK("没有查询到相关消息",listProcessBom,0);
            }
            //如果页数和页面数量都不为空的情况下进行计数
            Integer count= processExchangeMapper.countProcessExchange(processExchange);
            return JSONObject.oK("查询成功",listProcessBom,count);
        } catch (GlobalException e) {
            // 捕获异常,打印并返回数
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }
    }

    /**
     * 非空判断 长度验证   checkParam
     *      查重判断 checkParamsNotRepeat
     * 默认值赋值 setDataState(1)
     * 返回总数 count
     * @param processExchange
     * @return
     */
    @Override
    public JSONObject insertProcessExchange(ProcessExchange processExchange) {
        /**
         * 这里还要判断哪些不为空
         */
        try{
            if(processExchange.getId()!=null){
                    throw new ModifyFailedException("新增时ID不能存在");
            }
            if(processExchange.getDataState()==null){
                processExchange.setDataState(1);
            }
            checkParam(processExchange);
            processExchange.setDataState(1);
            Integer row=processExchangeMapper.insertProcessExchange(processExchange);
            if(row==0){
                throw new ModifyFailedException("新增不良现象失败");
            }
            return JSONObject.oK("新增成功");
        }catch(GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    @Override
    public JSONObject deleteProcessExchange(List<Integer> processExchangeIds, Integer IDataState) {
        try{
            //判断传入的参数是否为null
            if(IDataState == null) {
                //为null抛出异常
                throw new ParamsErrorException("数据状态不能为空");
            }else if(IDataState != 1 && IDataState != 2 && IDataState != 3) {
                throw new ParamsErrorException("数据状态错误");
            }
            if(processExchangeIds == null) {
                throw new ParamsErrorException("请选择设备资料");
            }
            //调用删除的接口
            int row = processExchangeMapper.deleteProcessExchange(processExchangeIds,IDataState);
            //声明msg字符串，用来存抛出的异常
            String msg = "";
            if (row == 0) {
                // 判断传入的数据状态参数lDataState,返回相应信息
                switch (IDataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                    default:
                        break;
                }
            } else {
                switch (IDataState) {
                    case 1:
                        msg = "启用成功";break;
                    case 2:
                        msg = "删除成功";break;
                    case 3:
                        msg = "停用成功";break;
                    default:
                        break;
                }
            }
            return JSONObject.oK(msg);
        }catch(GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    @Override
    public JSONObject updateProcessExchange(ProcessExchange processExchange) {
        try {
            //判断传入的参数是否为null
            if(processExchange.getOfflineId()==null) {
                //为null则抛出异常
                throw new ParamsErrorException("修改维修下线id不能为空");
            }
            if(processExchange.getDataState()==null){
                processExchange.setDataState(1);
            }
            checkParam(processExchange);

            //调用修改的接口
            int row = processExchangeMapper.updateProcessExchange(processExchange);
            if(row == 0) {
                throw new ModifyFailedException("数据修改失败");
            }
            return JSONObject.oK("数据修改成功");
        }catch (GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    @Override
    public JSONObject batchInsertProcessExchange(List<Integer> lProFlows, Integer lProVer, Integer offlineId) {
        try{
            if(lProVer==null){
                throw new ModifyFailedException("版本编号不存在");
            }
            if(offlineId==null){
                throw new ModifyFailedException("维修流程编号不存在");
            }
            if(lProFlows==null){
                throw new ModifyFailedException("流程编号不存在");
            }
            Integer row=0;
            for(int i=0;i<lProFlows.size();i++){
                ProcessExchange processExchange=new ProcessExchange();
                processExchange.setlProFlow(lProFlows.get(i));
                processExchange.setOfflineId(offlineId);
                processExchange.setlProVer(lProVer);
                processExchange.setDataState(1);
                Integer Counts = processExchangeMapper.checkProcessExchange(processExchange);
                if (Counts !=0) {
                    throw new ParamsErrorException("该数据已存在");
                }else{
                    row=row+processExchangeMapper.insertProcessExchange(processExchange);
                }
            }
            if(row==0){
                throw new ModifyFailedException("新增失败");
            }
            return JSONObject.oK("新增成功",row);
        }catch(GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    public void checkParam(ProcessExchange processExchange){
//        if(processExchange.getModerno()==null){
//            throw new ParamsErrorException("任务单号不能为空");
//        }
//        if(processExchange.getNmorderno()==null){
//            throw new ParamsErrorException("新任务单号不能为空");
//        }
//
//        if(StringUtils.isEmpty(processExchange.getMatId())){
//            throw new ParamsErrorException("产品编码不能为空");
//        }
//        if(StringUtils.isEmpty(processExchange.getNmatId())){
//            throw new ParamsErrorException("新产品编码不能为空");
//        }
        if(processExchange.getlProFlow()==null){
            throw new ParamsErrorException("工艺流程ID不能为空");
        }
        if(processExchange.getOfflineId()==null){
            throw new ParamsErrorException("维修下线ID不能为空");
        }
        if(processExchange.getlProVer()==null){
            throw new ParamsErrorException("版本ID不能为空");
        }
        // 名称不能重复
        Integer Counts = processExchangeMapper.checkProcessExchange(processExchange);
        if (Counts !=0) {
            throw new ParamsErrorException("该维修下线已存在");
        }
    }
}
